package com.example.demo.practice;

public class X的平方根 {


    public static int mySqrt(int x) {


        int i = 1;
        int j = x/2 + 1;
        while(i < j){
            int middle = (i+j)/2;
            if(middle * middle == x){
                return middle;
            }else if(middle > x/middle){
                j = middle;
            }else{
                i = middle;
            }
            if(j - i == 1){
                return i;
            }
        }
        return x;
    }

    public static void main(String[] args) {

        System.out.println(mySqrt(8));
    }



}
